import asyncio
import logging

logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

async def get_html(sleep_times):
    logging.info(" begin")
    await asyncio.sleep(sleep_times)
    logging.info(f"end after {sleep_times} s")

def test():
    loop = asyncio.get_event_loop()
    tasks = [get_html(2),get_html(3),get_html(3)]
    try:
        loop.run_until_complete(asyncio.wait(tasks))
    except KeyboardInterrupt as e:
        all_tasks = asyncio.Task.all_tasks()
        for t in all_tasks:
            print(f" 取消 {t}  {t.cancel()}")
        loop.stop()
        #必须执行下面一句否则报错
        loop.run_forever()
    finally:
        loop.close()

if __name__ == '__main__':
    test()